<template>
  <div class="header">
    <img src="@/assets/logo.webp" />
    <div class="title">RBAC Admin Ele</div>
    <div class="timer">{{ timeRef }}</div>
    <el-dropdown>
      <span class="el-dropdown-link">
        <!-- 用户名 -->
        {{ user.name }}
        <el-icon class="el-icon--right">
          <arrow-down />
        </el-icon>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item @click="logOut">退出登录</el-dropdown-item>
          <el-dropdown-item @click="dialogVisable = true"
            >修改密码</el-dropdown-item
          >
        </el-dropdown-menu>
      </template>
    </el-dropdown>
    <!-- 修改密码的表单 -->
    <el-dialog
      v-model="dialogVisable"
      title="修改密码"
      width="500"
      @close="close"
    >
      <!-- 表单 -->
      <el-form :model="passForm" :rules="rules" ref="formEle">
        <el-form-item prop="oldPass">
          <el-input
            type="password"
            placeholder="输入原始密码"
            v-model="passForm.oldPass"
          ></el-input>
        </el-form-item>
        <el-form-item prop="newPass">
          <el-input
            type="password"
            placeholder="输入新的密码"
            v-model="passForm.newPass"
          ></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
          <el-input
            type="password"
            placeholder="确认新的密码"
            v-model="passForm.checkPass"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitChange">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useUserStore } from "@/store/useUserStore"
import { useTimer } from "./useTimer"
import { storeToRefs } from "pinia"
import { useLogout } from "./useLogout"
import { useRouter } from "vue-router"
import { useDialog } from "./useDialog"

const router = useRouter()
const store = useUserStore()
const { user } = storeToRefs(store)

const { logOut } = useLogout(router, user)
const { dialogVisable, passForm, rules, formEle, submitChange, close } =
  useDialog(user, logOut)
const { fn, timeRef } = useTimer()
fn()
</script>

<style scoped lang="less">
.header {
  display: flex;
  padding: 20px;
  align-items: center;
  gap: 20px;
  img {
    width: 45px;
  }
  .timer {
    margin-left: auto;
  }
}
</style>
